import java.util.Arrays;

public class MyArrayList implements Iwork{

        public int[] array;
        public int usedSize;

        public MyArrayList() {
            this.array = new int[10];
        }

        @Override
        public String toString() {
            return "ArrayList{" +
                    "array=" + Arrays.toString(array) +
                    ", usedSize=" + usedSize +
                    '}';
        }

        //在末尾插入一个元素
        public void add(int date){
            //判断空间是否足够
            if(isFull()){
                grow();
            }
            array[this.usedSize]=date;
            this.usedSize++;

        }
        public void grow(){
            this.array=Arrays.copyOf(this.array,2*this.array.length);
        }
        public boolean isFull(){
            return this.usedSize== array.length;
        }
        //在pos处插入一个元素
        public void add(int pos,int date){

            for (int i = this.usedSize-1; i>= pos; i--) {
                array[i+1]=array[i];
            }
            array[pos]=date;
            this.usedSize++;
        }
        //判断是否包含toFind元素
        public boolean cotains(int toFind){
            for (int i = 0; i < this.usedSize; i++) {
                if(array[i]==toFind){
                    return true;
                }

            }
            return false;
        }
        //查找摸个元素对应的位置
        public int indexof(int toFind){
            for (int i = 0; i < this.usedSize; i++) {
                if(array[i]==toFind){
                    return i;
                }
            }
            return 0;
        }
        //获取pos位置对应的元素
        public int get(int pos){
            try{
                checkPos(pos);
                return array[pos];
            }catch (PosIllegal e){
                e.printStackTrace();
            }
            return -1;
        }
        //给pos位置处的元素设为value
        public void set(int pos,int value){
            try{
                checkEmpty();
                checkPos(pos);
                array[pos]=value;
            }catch (PosIllegal e){
                e.printStackTrace();
            }catch (EmptyIllegal e){
                e.printStackTrace();
            }
        }
        //删除第一次出现的元素toRemove
        public void remove(int toRemove){
            try{
                checkEmpty();
                int pos=indexof(toRemove);
                if(pos==-1){
                    return;
                }else{
                    for (int i = pos; i <this.usedSize-1 ; i++) {
                        array[i]=array[i+1];
                    }
                }
                this.usedSize--;
            }catch (EmptyIllegal e){
                e.printStackTrace();
            }
        }
        //返回当前顺序表的长度
        public int size(){
            return this.usedSize;

        }
        //清空顺序表
        public void clear(){
            this.usedSize=0;

        }
        //遍历，打印顺序表
        public void disPlay(){
            for (int i = 0; i <this.usedSize; i++) {
                System.out.print(array[i]+" ");
            }

        }
        public void checkPos(int pos){
            if(pos<0||pos>=usedSize){
                throw new PosIllegal("pos位置不合法");
            }
        }
        public void checkEmpty(){
            if(usedSize==0){
                throw new EmptyIllegal("空！！！！");
            }
        }
}
